package net.tycmc.cty.ctyms.timejob.iems.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.tycmc.functions.util.MapGetter;
import com.tycmc.functions.util.StringUtil;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.cty.ctyms.timejob.iems.dao.AllScanDao;
import net.tycmc.cty.ctyms.timejob.iems.dao.MtnProgarmDao;
import net.tycmc.cty.ctyms.timejob.iems.thread.ThreadTerminateMaint;

public class MtnProgramService{
	
	private  MtnProgarmDao mtnProgarmDao;
	
	
	public MtnProgarmDao getMtnProgarmDao() {
		return mtnProgarmDao;
	}

	public void setMtnProgarmDao(MtnProgarmDao mtnProgarmDao) {
		this.mtnProgarmDao = mtnProgarmDao;
	}

	public Map<String,Object> getLinkMan(Map<String,Object> params){
		
		String vehicleid = params.get("vehicleid")+"";//设备ID
		String MtnAsTpId = params.get("mtnas_tpid")+"";//保养类型
		
		Map<String,Object> paramET = new HashMap<String, Object>();
		paramET.put("vehicleId", vehicleid);
		paramET.put("mtnastpid", MtnAsTpId);
		List<Map<String,Object>> listET = mtnProgarmDao.getLastMtnEt(paramET);
		
		if(null!=listET&&listET.size()>0){
			Map<String,Object> mapET = listET.get(0);
			String nextRemart = mapET.get("MtnEt_NextRemark")+"";
			params.put("nextRemart",nextRemart);
		}else{
			params.put("nextRemart","");
		}
		
		if(StringUtil.isValid(vehicleid)){
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("vehicleId", vehicleid);
			List<Map<String,Object>> list = mtnProgarmDao.getMtnLinkManByParam(param);
			if(null!=list&&list.size()>0){
				for(int i=0;i<list.size();i++){
					Map<String,Object> map = list.get(i);
					String MtnLk_Type = map.get("MtnLk_Type")+"";
					if("1".equals(MtnLk_Type)){
						params.put("linkman1", map);
					}
					if("2".equals(MtnLk_Type)){
						params.put("linkman2", map);
					}
					if("3".equals(MtnLk_Type)){
						params.put("linkman3", map);
					}
				}
			}
			
		}
		
		
		return params;
	}
	/**
	 * 保养提醒
	 **/
	public void startWork() {
		//获取上一个七天的开始时间和结束时间
		ExecutorService executor = Executors.newFixedThreadPool(20);
		List<Future> futureList = new ArrayList<Future>();
		
		List<String> sqlList = new ArrayList<String>();
		List<Map<String,Object>> list = mtnProgarmDao.getMtnVehicleList();
		if(null!=list&&list.size()>0){
			for(int i=0,size=list.size();i<size;i++){
				Map<String,Object> map = list.get(i);
				map = getLinkMan(map);
				ThreadTerminateMaint  terminate = new ThreadTerminateMaint(map);
				Future<String> future = executor.submit(terminate);
				futureList.add(future);
			}
			try {
				for(Future futrue:futureList){
					String sqlString = (String) futrue.get();
					if(StringUtil.isValid(sqlString)){
						sqlList.add(sqlString);
					}
					
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		String sql = "";
		int num=0;
		int succss=0;
		if(null!=sqlList&&sqlList.size()>0){
			for(int i=0;i<sqlList.size();i++){
				String str = sqlList.get(i);
				if(sqlList.size()==1){
					succss++;
					int k= mtnProgarmDao.toQuerySql(str);
					if(k>0){
						num++;
					}
				}else{
					if(i!=0&&(i%5==0||i==sqlList.size()-1)){
						succss++;
						sql=sql+str;
						int k= mtnProgarmDao.toQuerySql(sql);
						if(k>0){
							num++;
						}
						sql = "";
					}else{
						sql=sql+str;
					}	
				}
				
			}
			System.out.println("本次应执行成功集合:"+succss+"个;实际执行成功的集合为:"+num+"个;"+(succss==num?"最终结果执行成功；":"最终结果执行失败；"));
		}
	}
}
